package edu.cth.scrabbit.server.core.network;

import java.util.List;

import edu.cth.scrabbit.server.core.ServerModel;
import edu.cth.scrabbit.server.core.ServerPlayer;
import edu.cth.scrabbit.shared.GameLetter;
import edu.cth.scrabbit.shared.network.Statement;
import edu.cth.scrabbit.shared.network.Statement.Statement_Type;

/**
 * 
 * A class that gets incoming statements from the clients and, depending what type
 * they are, do a certain action. 
 * 
 * @author Anders Jonsson, Marcus Harryson
 * 
 */
public class ServerProtocol {
	public static Statement processStatement(Statement incomingStatement,
			PlayerThread thread) {
		// TODO this method.
		ServerPlayer player = ServerModel.getInstance().getServerPlayer(thread);

		Statement_Type type = incomingStatement.getType();
		Statement reply = null;
		switch (type) {
		case CONNECT:
			String nickname = (String) incomingStatement.getContent();
			reply = ServerModel.getInstance().playerConnected(player, nickname);
			break;
		case START_GAME:
			reply = ServerModel.getInstance().startGame(player);
			break;
		case PASS:
			reply = ServerModel.getInstance().pass(player);
			break;
		case SWAP:
			reply = ServerModel.getInstance().swap(player);
			break;
		case PLAY_WORD:
			reply = ServerModel.getInstance().play(player,
					(List<GameLetter>) incomingStatement.getContent());
			break;
		case PING:
			reply = ServerModel.getInstance().getGameStateStatement(player);
			break;
		}

		return reply;
	}
}
